<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="结果名称" prop="resultName">
        <el-input
          v-model="queryParams.resultName"
          placeholder="请输入结果名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="算法类型" prop="algorithmType">
        <el-select v-model="queryParams.algorithmType" placeholder="请选择算法类型" clearable>
          <el-option
            v-for="dict in dict.type.algorithm_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50"/>
      <el-table-column label="结果名称" align="center" prop="resultName" width="180" show-overflow-tooltip />
      <el-table-column label="内容图示" align="center" prop="illustrationName" width="130" show-overflow-tooltip>
        <template slot-scope="scope">
          <image-preview :src="scope.row.illustrationPath" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="算法类型" align="center" prop="algorithmType" width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.algorithm_type" :value="scope.row.algorithmType"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-more"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改算法运行结果对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="88px" class="form-overflow-hide">
        <el-row>
          <el-col :span="12">
            <el-form-item label="结果名称" prop="resultName">
              <el-input v-model="form.resultName" placeholder="请输入结果名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="算法类型" prop="algorithmType">
              <el-select v-model="form.algorithmType" placeholder="请选择算法类型" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.algorithm_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="内容图示" prop="illustrationName">
              <el-input v-model="form.illustrationName" placeholder="请上传图示" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="上传图示" prop="illustrationPath">
              <image-upload :limit="1" @multipleInput="getImgFileUpload" :open-time="new Date().getTime()" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="结果内容">
          <editor v-model="form.resultContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="结果说明">
          <editor v-model="form.resultExplain" :min-height="192"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="255" show-word-limit />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="运行结果详情" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="88px" class="form-overflow-hide">
        <el-row>
          <el-col :span="12">
            <el-form-item label="结果名称" prop="resultName">
              {{ form.resultName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="算法类型" prop="algorithmType">
              <dict-tag :options="dict.type.algorithm_type" :value="form.algorithmType"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="内容图示" prop="illustrationName">
              <ImagePreview :src="form.illustrationPath" :width="'100%'" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="结果内容">
          <div v-html="form.resultContent" />
        </el-form-item>
        <el-form-item label="结果说明">
          <div v-html="form.resultExplain" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          {{ form.remark }}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="detailOpen = false">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listResult, getResult, delResult, addResult, updateResult } from "@/api/knowledge/result";

export default {
  name: "Result",
  dicts: ['module_type', 'algorithm_type', 'sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 算法运行结果表格数据
      resultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      detailOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        resultName: null,
        resultContent: null,
        resultExplain: null,
        illustrationName: null,
        illustrationPath: null,
        moduleType: null,
        algorithmType: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        resultName: [
          { required: true, message: "结果名称不能为空", trigger: "blur" }
        ],
        resultContent: [
          { required: true, message: "结果内容不能为空", trigger: "blur" }
        ],
        resultExplain: [
          { required: true, message: "结果说明不能为空", trigger: "blur" }
        ],
        illustrationName: [
          { required: true, message: "内容图示没有上传", trigger: "blur" }
        ],
        algorithmType: [
          { required: true, message: "算法类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询算法运行结果列表 */
    getList() {
      this.loading = true;
      this.queryParams.moduleType = sessionStorage.getItem('moduleType')
      listResult(this.queryParams).then(response => {
        this.resultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.detailOpen = false
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        resultId: null,
        resultName: null,
        resultContent: null,
        resultExplain: null,
        illustrationName: null,
        illustrationPath: null,
        moduleType: null,
        algorithmType: null,
        delFlag: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.form.moduleType = sessionStorage.getItem('moduleType')
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.resultId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加算法运行结果";
    },
    handleDetail(row) {
      this.reset();
      const resultId = row.resultId || this.ids
      getResult(resultId).then(response => {
        this.form = response.data;
        this.detailOpen = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const resultId = row.resultId || this.ids
      getResult(resultId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改算法运行结果";
      });
    },
    getImgFileUpload(res) {
      if (res.length > 0) {
        let file = res[0]
        this.form.illustrationName = file.originalFilename
        this.form.illustrationPath = file.fileName
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.resultId != null) {
            updateResult(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResult(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const resultIds = row.resultId || this.ids;
      this.$modal.confirm('是否确认删除算法运行结果编号为"' + resultIds + '"的数据项？').then(function() {
        return delResult(resultIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('knowledge/result/export', {
        ...this.queryParams
      }, `result_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.form-overflow-hide {
  height: 70vh;
  padding: 8px;
  overflow-y: auto;
}
</style>
